$(function() {

  $('#new_profile').bootstrapValidator({
    message: 'This value is not valid',
    feedbackIcons: {
      valid: 'glyphicon glyphicon-ok',
      invalid: 'glyphicon glyphicon-remove',
      validating: 'glyphicon glyphicon-refresh'
    },
    submitButtons: 'input[type="submit"]',
    fields: {
      'profile[name]': {
        message: 'The name is not valid',
        validators: {
          notEmpty: {
            message: 'Name is required and cannot be empty'
          },
          stringLength: {
            min: 4,
            max: 30,
            message: 'Name is between 4 and 30 characters long'
          },
          regexp: {
            regexp: /^[a-zA-Z\s]+$/,
            message: 'Name can only consist of alphabetical and space'
          }
        }
      },

      'profile[gender]': {
        validators: {
          notEmpty: {
            message: 'Gender is required and cannot be empty'
          },
        }
      },

      'profile[born]': {
        message: '',
        validators: {
          date: {
            format: 'YYYY-MM-DD',
            message: 'The value is not a valid date'
          },

          notEmpty: {
            message: 'Birthday is required and cannot be empty'
          },
        }
      },

      'profile[weight]': {
        validators: {
          notEmpty: {
            message: 'Weight is required and cannot be empty'
          },
          integer: {
            message: 'The value is not an integer'
          }
        }
      },

      'profile[height_feet]': {
        validators: {
          notEmpty: {
            message: 'Height (feet) is required'
          },
          integer: {
            message: 'The value is not an integer'
          }
        }

      },

      'profile[height_inches]': {
        validators: {
          notEmpty: {
            message: 'Height (inches) is required'
          },
          integer: {
            message: 'The value is not an integer'
          }
        }
      },
    }
  });
});



////////////////////////////////////////////////////////////////
$(function() {

  $("#datepicker-dob").datepicker({
    format: "yyyy-mm-dd",
    startView: "decade",
    startDate: "-100y",
    endDate: "-1y",
    weekStart: "1",
    autoclose: true,
    todayHighlight: true
  });
  
  $('#datepicker-dob').on('change', function(e) {
    // Validate the date when user change it
    $('#new_profile')
    // Get the bootstrapValidator instance
    .data('bootstrapValidator')
    // Mark the field as not validated, so it'll be re-validated when the user change date
    .updateStatus('profile[born]', 'NOT_VALIDATED', null)
    // Validate the field
    .validateField('profile[born]');
  });

}); 
